///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IO_CTEXTREADER_H_
#define _BUOLA_IO_CTEXTREADER_H_

#include <buola/io/cbufferstream.h>

namespace buola { namespace io {

class CTextReader
{
public:
    CTextReader(PBufferStream pStream)
        :   mStream(pStream)
    {}
    
    CTextReader(const CURI &pURI,EMode pMode=EMode::READ);

    CResult Read(char *pData,size_t pLen)
    {
        return mStream->Read((uint8_t*)pData,pLen);
    }
    
    CResult Ignore(size_t pLen,char pChar)
    {
        return mStream->Ignore(pLen,pChar);
    }
    
    bool Peek(char &pChar)
    {
        uint8_t lByte;
        bool lResult=mStream->Peek(lByte);
        pChar=lByte;
        return lResult;
    }
    
    void Bump()
    {
        mStream->Bump();
    }
    
    CTextReader &operator>>(std::string &pString);
    CTextReader &operator>>(std::u16string &pString);
    CTextReader &operator>>(std::wstring &pString);

    CTextReader &operator>>(char &pChar);
    
    CTextReader &operator>>(uint8_t &pInt);
    CTextReader &operator>>(int8_t &pInt);
    CTextReader &operator>>(uint16_t &pInt);
    CTextReader &operator>>(int16_t &pInt);
    CTextReader &operator>>(uint32_t &pInt);
    CTextReader &operator>>(int32_t &pInt);
    CTextReader &operator>>(uint64_t &pInt);
    CTextReader &operator>>(int64_t &pInt);

    CTextReader &operator>>(float &pReal);
    CTextReader &operator>>(double &pReal);
    CTextReader &operator>>(long double &pReal);
    
    const PBufferStream &Stream()   {   return mStream; }

private:
    PBufferStream mStream;
};

inline bool get_line(CTextReader &pStream,std::string &pString,const char pDelim='\n')
{
    return get_line(pStream.Stream(),pString,pDelim);
}

inline bool get_line(CTextReader &pStream,std::wstring &pString,const char pDelim='\n')
{
    return get_line(pStream.Stream(),pString,pDelim);
}

/*namespace io*/ } /*namespace buola*/ }

#endif
